﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>选择用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <script src="../../../layuiadmin/lib/jquery.min.js"></script>
    <script src="../../../layuiadmin/lib/common.js"></script>
    <link href="../../../layuiadmin/style/mystyle.css" rel="stylesheet" />
    <script src="../../../layuiadmin/lib/api.js"></script>
    <script src="../../../layuiadmin/layui/layui.js"></script>
    <script src="../../../layuiadmin/lib/system.js"></script>
    <script>
        sys_islogin();
    </script>
</head>
<body>

    <div class="layui-form" lay-filter="layuiadmin-form-role" id="layuiadmin-form-role" style="padding: 20px 30px 0 0;">
        <div id="select_depart">

        </div>
        
        <div class="layui-form-item layui-layout-admin" style="text-align:center;">
            <div class="layui-footer-new" style="padding:0px;background-color:#fff;">
                <button class="layui-btn" lay-submit lay-filter="LAY-user-select-submit" id="LAY-user-select-submit">确定</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="close">取消</button>
            </div>
        </div>
        
    </div>
    <input type="hidden" id="parent_text_id" />
    <input type="hidden" id="parent_hidden_id" />
    
    <script>
        var parentid = "";

        //设置用户和部门
        $.ajax({
            url: "/UserMerber/GetDepartAndUser",    //请求的url地址
            dataType: "json",   //返回格式为json
            async: false,//请求是否异步，默认为异步，这也是ajax重要特性$("#attachCode").val()
            data: {},    //参数值
            type: "POST",   //请求方式
            success: function (res) {
                if (res.departlist.length > 0) {
                    var html = "";
                    for (var i = 0; i < res.departlist.length; i++) {
                        html += " <div class=\"layui-form-item\">";
                        html += "<label class=\"layui-form-label\" >" + res.departlist[i].NAME+"</label >";
                        html += "<div class=\"layui-input-block\">";
                        for (var j = 0; j < res.userlist.length; j++) {
                            if (res.userlist[j].DEPARTID == res.departlist[i].ID) {
                                html += "<input lay-filter=\"checkbox_user\" type=\"checkbox\" name=\"users\" lay-skin=\"primary\" title=\"" + res.userlist[j].REALNAME + "\" value=\"" + res.userlist[j].ID+"\">";
                            }
                        }
                        html += "</div>";
                        html += "</div>";
                    }
                    $("#select_depart").html(html);
                }
            },
            error: function (res) {
                console.log(res);
                //layer.msg("任务附件获取失败，请联系技术人员");
            }
        });
        
        //接收父页面传递的参数
        //需要的参数 父页面数据接受框的id，选择的数据
        function child_select_user(parentid,hiddenid, value) {
            parentid = parentid;
            $("#parent_text_id").val(parentid); //文本显示框的id
            $("#parent_hidden_id").val(hiddenid); //隐藏文本保存用户名的id

            //设置选中
            common.setcheckboxselect("users", value);
        };
        layui.config({
            base: '../../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form'], function () {


            var $ = layui.$, form = layui.form;
            var arr_box = [];
            var arr_text = [];
            //监听提交
            form.on('submit(LAY-user-select-submit)', function (data) {
                //var field = data.field; //获取提交的字段

                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                //$.ajax({});
                var text = common.selecttext("users");
                var val = common.selectvaluebyName("users", "noTrim");

                //console.log(text + "  " + val);
                //给显示文本赋值
                var id = '#' + $("#parent_text_id").val();
                parent.layui.$(id).val(text.toString());

                
                if (val) {
                    //添加前后逗号
                    val = ',' + val + ',';
                }
                //给隐藏文本赋值
                var id = '#' + $("#parent_hidden_id").val();
                parent.layui.$(id).val(val.toString());

                parent.layer.close(index); //再执行关闭
                return false; //阻止表单跳转
            });

            
        })

        //关闭弹窗
        $("#close").on("click", function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        });
    </script>
</body>
</html>